<?php
/**
 * Created by PhpStorm.
 * User: jx
 * Date: 2018-03-22
 * Time: 21:35
 */

namespace app\common\logic;
use think\Model;

use app\common\logic\Logic;
use app\common\model\Module;
use app\common\model\Category;
use app\common\model\Type;
use app\common\model\CatType;

use think\Cache;
class CategoryLogic extends Logic
{
    protected $modelClass = 'app\common\model\Category';
    protected $column = 'moduleid,parentid,id,hits,title';
    protected $order = 'listorder desc';
    
   
    
    public function getTypeName($moduleid){
        $module = Module::get($moduleid);
        if(!$module){
            exception('模块未知');
        }
        return $module['name'];
    }
    
    /**
     * @param $catid
     * @desc 获取菜单关联的叶子类目
     */
    public function getTypes($catid){
        $list = CatType::all(['category_id'=>$catid]);
        $str = '';
        foreach ($list as $type){
            if($str===''){
                $str .= $type->type_title;
            }else{
                $str .= ','.$type->type_title;
            }
            
        }
        return $str;
    }
    
    public function getLevel($parentid){
        if($parentid==0){
            return 1;
        }
        $parent = Category::get($parentid);
        if(!$parent||!$parent['level']){
            exception('父级类目没有级别信息');
        }
        return $parent['level']+1;
        
    }
    
    public function getBindTypeList($cid){
        $bindList = CatType::where('category_id',$cid)->field('id,type_id')->select();
        $catIds = [];
        foreach ($bindList as $catId){
            $catIds[] = $catId->type_id;
        }

        $lists = Type::where('id','in',$catIds)->field('id,title')->select();
       
        return $lists;
    }
    
    
    public function getFoldList($len=1,$moduleid=1){
    
        
        $this->condition['moduleid'] = $moduleid;
        if(Cache::get('categoryFoldList'.$moduleid.$len)){
            $rt = Cache::get('categoryFoldList'.$moduleid.$len);
            return callsuccess($rt);
        }

        $rt = [];
        $this->size = null;
        $this->field = 'id,title,thumb';
        $this->condition['parentid'] = 0;
        
        $one = $this->prepareDataProvider();
       
        
        //只要一级
        if($len==1){
            $rt = $one;
            Cache::set('categoryFoldList'.$moduleid.$len,$rt);
            return callsuccess($rt);
        }
        
        foreach ($one as $menu1){
            $this->condition['parentid'] = $menu1['id'];
            $two = $this->prepareDataProvider();
            $menu1['child'] = [];
            //只要二级
            if($len==2){
                $menu1['child'] = $two;
                $rt[] = $menu1;
                continue;
            }
            foreach ($two as $menu2){
                $this->condition['parentid'] = $menu2['id'];
                $menu2['child'] = [];
                $menu2['child'] = $this->prepareDataProvider();
                $menu1['child'] = $two;
            }
            $rt[] = $menu1;
        }
    
        
        Cache::set('categoryFoldList'.$moduleid.$len,$rt);
        return callsuccess($rt);
        
    }
    
    public function getTabelData($len=1,$moduleid=1){
        
        
        $this->condition['moduleid'] = $moduleid;
       
        
        $rt = [];
        $this->size = null;
        $this->field = 'id,title';
        $this->condition['parentid'] = 0;
        
        $one = $this->prepareDataProvider();
        
        
        //只要一级
        if($len==1){
            $rt = $one;
            return callsuccess($rt);
        }
        
        foreach ($one as $menu1){
            $this->condition['parentid'] = $menu1['id'];
            $two = $this->prepareDataProvider();
            $menu1['nodes'] = [];
            //只要二级
            if($len==2){
                $menu1['child'] = $two;
                $rt[] = $menu1;
                continue;
            }
            foreach ($two as $menu2){
                $this->condition['parentid'] = $menu2['id'];
                $menu2['nodes'] = [];
                $menu2['nodes'] = $this->prepareDataProvider();
                $menu1['nodes'] = $two;
            }
            $rt[] = $menu1;
        }
        
        return callsuccess($rt);
        
    }
    
    

    

}